package battleship;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;

public class JoinGameServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // have one user join another user's game and let the games begin!
        System.out.println("JoinGameS: starting");
    	String userToken= request.getParameter("token");
    	int userID= (Integer) UserIdTokenHashMap.idTokenMap.get(userToken);
        // get the user's ID and the opponent's id, get the corresponding users
    //    int userID = Integer.parseInt(request.getParameter("id"));
        User user = Context.users[userID];
        int opponentID = Integer.parseInt(request.getParameter("opponent"));
        User opponent = Context.users[opponentID];
        
        Game game = opponent.game;
        game.player2ID = userID;
        user.game = game;
        
        user.playing = true;
        opponent.playing = true;
        user.inLobby = false;
        opponent.inLobby = false;
        user.opponentID = opponentID;
        opponent.opponentID = userID;

        // set the starting player
        if (game.startingPlayer.equals("player1")) {
            opponent.startingPlayer = true;
            user.startingPlayer = false;
        } else {
            opponent.startingPlayer = false;
            user.startingPlayer = true;
        }
        
        // notify the player who started the game that someone has joined them
        try {
            opponent.waitForJoin.put(user);
        } catch (InterruptedException e) {
            System.out.println("JoinGameServlet interruptedexception: " + e.getMessage());
        }

        // update global lists
        Context.openGames.remove(game);
        Context.currentGames.add(game);

        // tell other users about it
        Iterator<User> userIterator = Context.onlineUsers.iterator();
        while(userIterator.hasNext()) {
            User otherUser = userIterator.next();
            if (otherUser.inLobby) {
                try {
                    otherUser.oldOpenGames.put(game);
                    otherUser.newRunningGames.put(game);
                } catch (InterruptedException e) {
                    System.out.println("JoinGameServlet interrupted2: " + e.getMessage());
                }
                
            }
        }

//        response.setContentType("text/json");
//        PrintWriter out = response.getWriter();
//        out.print("{\"startingPlayer\":" + user.startingPlayer + "}");
//        out.close();
        System.out.println("JoinGameServlet: finished");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
